/*
 * @Author: your name
 * @Date: 2021-09-30 09:13:05
 * @LastEditTime: 2021-09-30 18:15:24
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \wx-x-mai\pages\web\web.js
 */
const FormData = require("../../plugins/formData/formData")
const deleteObjProperty = (arr, obj) => {
	arr.forEach((item) => {
		delete obj[item]
	})
}

const deleteSchoolConfig = ["picUrlArr", "iconUrl", "schoolName"]
const deleteUserInfo = ["cardNumber", "identityTitle", "schoolCode"]

const app = getApp()
Page({
	data: {
		path: "",
	},
	onLoad(options) {
		try {
			let { path, color, f, encode, eventType, Dfrom } = options
			console.log(Dfrom, "Dfrom")
			f = f || "f"
			// const path = 'http://192.168.7.119:3000/#/activity'
			// const path = 'http://xwx.gzzmedu.com:6999/x-mai/#/activity'
			console.log(options, "web-view options")
			if (color) {
				wx.setNavigationBarColor({
					frontColor: `#${f}${f}${f}${f}${f}${f}`,
					backgroundColor: `#${color}`,
				})
			}
			const userInfo = wx.getStorageSync("userInfo")
			const schoolConfig = wx.getStorageSync("schoolConfig")
			const token = wx.getStorageSync("token")

			deleteObjProperty(deleteSchoolConfig, schoolConfig)
			deleteObjProperty(deleteUserInfo, userInfo)

			const query = encodeURIComponent(
				JSON.stringify({
					userInfo,
					schoolConfig,
					token,
				}),
			)
			let url = ""
			if (decodeURIComponent(path).indexOf("?") !== -1) {
				url = `${decodeURIComponent(
					path,
				)}&query=${query}&t=${new Date().getTime()}`
			} else {
				url = `${decodeURIComponent(
					path,
				)}?query=${query}&t=${new Date().getTime()}${
					Dfrom ? "&from=" + Dfrom : ""
				}`
			}
			if (encode === "true") {
				// path = decodeURIComponent(path)
				// let newpath = path.split('?')[0]
				// let newquery = path.split('?')[1]
				url = `${decodeURIComponent(path)}&t=${new Date().getTime()}`
			}
			// 文件代办和文件翻译
			if (
				path === "https://xwx.gzzmedu.com/file-agency/" ||
				"https://xwx.gzzmedu.com/file-translate/"
			) {
				app.globalData.isHzdb = true
			} else if (
				path === "https://xwx.gzzmedu.com/advertising" &&
				eventType !== "click"
			) {
				wx.$API["home/addRecord"]({
					menuId: "ffe479d8-2ad8-49c4-9fd7-aab7d0ef5f3",
				}).catch((err) => {
					console.log(err)
				})
			}
			console.log(url, "url")
			this.setData({
				path: url,
			})
		} catch (error) {
			console.log(error)
			wx.showToast({
				title: "参数错误",
				icon: "none",
				duration: 2000,
			})
			wx.navigateBack({
				delta: 1,
			})
		}
	},
	onUnload() {
		if (
			this.data.path.split("?").shift() ===
			"https://xwx.gzzmedu.com/file-agency/"
		) {
			app.globalData.isHzdb = false
		}
	},
	getMsgHandle(e) {
		let data = JSON.parse(e.detail.data[0])
		console.log("回来了", data)
		if (this.data.path.includes("https://xwx.gzzmedu.com/advertising")) {
			let obj = JSON.parse(e.detail.data[0])
			console.log(obj, "getMsgHandle")
			let formData = new FormData()
			formData.append("logicType", obj.logicType)
			formData.append("pageName", obj.pageName)
			formData.append("pageUrl", obj.pageUrl)
			formData.append("id", obj.id)
			let data = formData.getData()

			wx.request({
				url: "https://xwx.gzzmedu.com:9080/api/order-service-ms/advertising/advertisingCount",
				method: "post",
				data: data.buffer,
				header: {
					"content-type": data.contentType,
				},
				success(res) {
					console.log(res.data)
				},
			})
			// 从文件代办跳回来
		} else if (data.origin === "fileAgency") {
			console.log("从文件代办跳转过来打开: " + data.open)
			// 打开粤东公证小程序
			if (data.open === "gzfw") {
				let gzfwdata = {
					schoolCode: wx.getStorageSync("schoolConfig").schoolCode,
					userId: wx.getStorageSync("userInfo").userId,
					origin: "xwx",
				}
				console.log(gzfwdata, "这是跳转公证服务带的参数")
				wx.navigateToMiniProgram({
					appId: "wx3b45a1e961170bfc",
					path: "/pages/home/home",
					extraData: gzfwdata,
				})
				// 打开校务行督学刷题小程序
			} else if (data.open === "DuXueShuaTi") {
				wx.navigateToMiniProgram({
					appId: "wxfa108ee7b0593fef",
					path: "/pages/index/index?channelId=2",
				})
			}
		}
	},
	onShareAppMessage() {
		// 党建新颜
		if (this.data.path.indexOf("createParty100YearXWX") !== -1) {
			return {
				title: "点击领取你的国庆专属头像框~",
				imageUrl: "https://xwx.gzzmedu.com:6899/weixin_sharedata.png",
				path: "/pages/web/web?color=BE3C3D&path=https://kou.huaweiai.cn/html/createParty100YearXWX.html",
			}
		} else {
			return {}
		}
	},
})
